<template>
  <!-- 新鲜好物组件 -->
  <div class="home-new">
    <home-panel title="新鲜好物" subTitle="新鲜出炉 品质靠谱">
      <template #right>
        <xtx-more path="/" />
      </template>
      <!-- 面板内容 -->
      <div style="position:relative;height:406px;" ref="target">
        <transition name="fade">
          <ul class="goods-list" v-if="goods.length">
            <li v-for="item in goods" :key="item.id">
              <router-link :to="`/product/${item.id}`">
                <img v-lazy="item.picture" alt="">
                <p class="name ellipsis">{{item.name}}</p>
                <p class="price">&yen;{{item.price}}</p>
              </router-link>
            </li>
          </ul>
          <home-skeleton bg="#f0f9f4" v-else />
        </transition>
      </div>
    </home-panel>
  </div>
</template>

<script>
import homePanel from './home-panel.vue'
import { findNew } from '@/api/home'
import HomeSkeleton from './home-skeleton.vue'
import { useLazyData } from '@/hooks'
export default {
  name: 'HomeNew',
  components: { homePanel, HomeSkeleton },
  setup () {
    // const goods = ref([])
    // const box = ref(null)
    // // stop 是停止观察是否进入或移出可视区域的行为
    // const { stop } = useIntersectionObserver(
    //   // box 是观察的目标dom容器，必须是dom容器，而且是vue3.0方式绑定的dom对象
    //   box,
    //   // isIntersecting 是否进入可视区域，true是进入 false是移出
    //   // observerElement 被观察的dom
    //   ([{ isIntersecting }]) => {
    //     if (isIntersecting) {
    //       stop()
    //       // 调用接口获取数据
    //       findNew().then(data => {
    //         goods.value = data.result
    //       })
    //     }
    //   }
    // )
    const { target, result } = useLazyData(findNew)
    return { goods: result, target }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
